package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.entity.Category;
import com.tjetc.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /***
     * 添加菜系
     * @param category
     * @return
     */
    @RequestMapping("/add")
    public JsonResult addCategory(Category category){
        LocalDateTime now = LocalDateTime.now();
        category.setCreateTime(now);
        int row = categoryService.insert(category);
        if(row<=0){
            return new JsonResult(1,"插入失败",row);
        }else {
            return new JsonResult(0,"插入成功",row);
        }
    }

    /***
     * 删除菜系
     * @param id 菜系id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public JsonResult deleteCategory(@PathVariable("id") Integer id){
        int row = categoryService.deleteById(id);
        if(row>0){
            return new JsonResult(0,"删除成功",row);
        }else{
            return new JsonResult(1,"删除失败",row);
        }
    }

    /***
     * 查询菜系
     * @param id 菜系id
     * @return
     */
    @RequestMapping("/find")
    public JsonResult findCategory(Integer id){
        Category category = categoryService.select(id);
        if(category==null){
            return new JsonResult(1,"菜系种类不存在",category);
        }else{
            return new JsonResult(0,"",category);
        }
    }

    /***
     * 查询菜系
     * @param merchantId 商家id
     * @return
     */
    @RequestMapping("query/{merchantId}")
    public JsonResult queryByMerchantId(@PathVariable int merchantId){
        List<Category> categories = categoryService.selectByMerchantId(merchantId);
        if(categories!=null){
            return new JsonResult(0,"查询成功",categories);
        }else {
            return new JsonResult(1,"查询失败，对象不存在",categories);
        }
    }
}
